package com.pzc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.luojun.entity.User;
import com.mysql.cj.Session;
import com.pzc.entity.PersonManage;
import com.pzc.service.PersonManageService;
import com.pzc.service.UserService;

/**
 * Servlet implementation class DoLogin
 */
@WebServlet("/DoLogin")
public class DoLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		
		String error="";
//		Integer userId= Integer.parseInt(request.getParameter("userId"));
		String username=request.getParameter("regName");//获得用户名
		String password=request.getParameter("regPwdRepeat");//获得密码
		User u =new User();
//		u.setUserId(userId);
		u.setUserName(username);
		u.setPassword(password);
		
		System.out.println(username);
		System.out.println(password);
		//登录，判断密码是否正确，正确成功，错误失败
		UserService us = new UserService();
		
		
	
		
		
		if(us.loginistrue(username, password)){
			u=us.login(username,password);
			session.setAttribute("user", u);
			System.out.println("DoLogin登录成功！");
			PersonManageService pms = new PersonManageService();
			PersonManage  pm  =pms.selectManage(u.getUserId());
			session.setAttribute("user", u);
			session.setAttribute("nickname", pm.getNickname());
			request.getRequestDispatcher("ToIndex").forward(request, response);//""里面写jsp的路径,首页路径
		}else{
			System.out.println("密码错误");
			error="错误密码";
			request.setAttribute(error, "error");
			request.getRequestDispatcher("ToLogin").forward(request, response);//""里面写jsp的路径
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
	}

}
